package com.cch.android.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.widget.ImageView;
import android.widget.Toast;

public class Constant {
	
	public static int m_screenWidth;
	public static int m_screenHeight;
	public static String activityName;
	public static String newPath;
	public static String fileName;
	
	private static LinkedList<String> extens=null;
	
	/**
	 * 是否已经扫描
	 */
	public static boolean isScaned;
	
	/**
	 * 全部图片文件夹
	 */
	public static ArrayList<ImageFolderInfo> imageFolders = new ArrayList<ImageFolderInfo>();
	
	/**
	 * 图片文件夹实体
	 * @author wanghb
	 *
	 */
	public static class ImageFolderInfo{
		public String path;
		public int pisNum = 0;
		public ArrayList<String> filePathes = new ArrayList<String>();
		public Drawable image;
	}
	
	public static LinkedList<String> getExtens(){
		if(extens == null){
			extens = new LinkedList<String>();
			extens.add("JPEG");
			extens.add("JPG");
			extens.add("PNG");
			extens.add("GIF");
			extens.add("BMP");
		}
		return extens;
	}
	
	
	//显示实体
	public static class gridItemEntity{
		public Drawable image;
		public String path;
		public int index;
		public ImageView imageView;
	}
	/**
	 * 取得sdk版本号
	 * @return
	 */
	 public static int getAndroidSDKVersion() {  
        int version = 0;  
        try {  
            version = Integer.valueOf(android.os.Build.VERSION.SDK);  
        } catch (NumberFormatException e) {  
        }  
        return version;  
    }  
	 /**
	  * 获取图片
	  * @param mPathes
	  * @param index
	  * @param zoom
	  * @return
	  */
	 public static Bitmap getDrawable(ArrayList<String> mPathes,int index, int zoom) {
			if (index >= 0 && index < mPathes.size()) {
				String path = mPathes.get(index);

				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeFile(path, options);
				int mWidth = options.outWidth;
				int mHeight = options.outHeight;
				int s = 1;
				while ((mWidth / s > m_screenWidth * 2 * zoom) || (mHeight / s > m_screenHeight * 2 * zoom)) {
					s *= 2;
				}

				options = new BitmapFactory.Options();
				options.inPreferredConfig = Config.ARGB_8888;
				options.inSampleSize = s;
				Bitmap bm = BitmapFactory.decodeFile(path, options);

				if (bm != null) {
					int h = bm.getHeight();
					int w = bm.getWidth();

					float ft = (float) ((float) w / (float) h);
					float fs = (float) ((float) m_screenWidth / (float) m_screenHeight);

					int neww = ft >= fs ? m_screenWidth * zoom : (int) (m_screenHeight * zoom * ft);
					int newh = ft >= fs ? (int) (m_screenWidth * zoom / ft) : m_screenHeight * zoom;

					float scaleWidth = ((float) neww) / w;
					float scaleHeight = ((float) newh) / h;

					Matrix matrix = new Matrix();
					matrix.postScale(scaleWidth, scaleHeight);
					bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
					return bm;
				}
			}
			return null;
		}
	 /**
	  * 获取图片
	  * @param mPathes
	  * @param index
	  * @param zoom
	  * @return
	  */
	 public static Bitmap getDrawable2(String path,int zoom) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeFile(path, options);
				int mWidth = options.outWidth;
				int mHeight = options.outHeight;
				int s = 1;
				while ((mWidth / s > m_screenWidth * 2 * zoom) || (mHeight / s > m_screenHeight * 2 * zoom)) {
					s *= 2;
				}

				options = new BitmapFactory.Options();
				options.inPreferredConfig = Config.ARGB_8888;
				options.inSampleSize = s;
				Bitmap bm = BitmapFactory.decodeFile(path, options);

				if (bm != null) {
					int h = bm.getHeight();
					int w = bm.getWidth();

					float ft = (float) ((float) w / (float) h);
					float fs = (float) ((float) m_screenWidth / (float) m_screenHeight);

					int neww = ft >= fs ? m_screenWidth * zoom : (int) (m_screenHeight * zoom * ft);
					int newh = ft >= fs ? (int) (m_screenWidth * zoom / ft) : m_screenHeight * zoom;

					float scaleWidth = ((float) neww) / w;
					float scaleHeight = ((float) newh) / h;

					Matrix matrix = new Matrix();
					matrix.postScale(scaleWidth, scaleHeight);
					bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
					return bm;
				}
			return null;
		}
	/**
	 * 把Bitmap转Byte
	 * 
	 * @Author HEH
	 * @EditTime 2010-07-19 上午11:45:56
	 */
	public static byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
	 /** 
     * @param 图片缩放 
     * @param bitmap 对象 
     * @param w 要缩放的宽度 
     * @param h 要缩放的高度 
     * @return newBmp 新 Bitmap对象 
     */  
    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){  
        int width = bitmap.getWidth();  
        int height = bitmap.getHeight();  
        Matrix matrix = new Matrix();  
        float scaleWidth = ((float) w / width);  
        float scaleHeight = ((float) h / height);  
        matrix.postScale(scaleWidth, scaleHeight);  
        Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,  
                matrix, true);  
        return newBmp;  
    }  

	/**
	 * 判断是否有sd卡
	 * 
	 * @return
	 */
	public static String getSDPath() {
		File SDdir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			SDdir = Environment.getExternalStorageDirectory();
		}
		if (SDdir != null) {
			return SDdir.toString();
		} else {
			return null;
		}
	}

    //然后创建文件夹-->
    public static  void createSDCardDir(Context cxt){
                if(getSDPath()==null){
                        Toast.makeText(cxt, "未找到SD卡", 2000).show();
                }else{
                            if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
                                // 创建一个文件夹对象，赋值为外部存储器的目录
                                 File sdcardDir =Environment.getExternalStorageDirectory();
                               //得到一个路径，内容是sdcard的文件夹路径和名字
                                  newPath=sdcardDir.getPath()+"/imageRome/app/tempImages/";//newPath在程序中要声明
                                 File path1 = new File(newPath);
                                if (!path1.exists()) {
                                 //若不存在，创建目录，可以在应用启动的时候创建
                                 path1.mkdirs();
                                 System.out.println("paht ok,path:"+newPath);
                               }
                         }
                         else{
                          System.out.println("false");
                        }
                }
            }
    /**
     * 保存图片到sd卡
     */
	public static String saveMyBitmap(String bitName, int percent,Drawable drawable) throws IOException {

		Bitmap bmp = drawable2Bitmap(drawable);// 这里的drawable2Bitmap方法是我把ImageView中
		String filePath = newPath + bitName + ".jpg" ;													// 的drawable转化成bitmap，当然实验的时候可以自己创建bitmap
		File f = new File(filePath);
		f.createNewFile();
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		bmp.compress(Bitmap.CompressFormat.JPEG, percent, fOut);
		try {
			fOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filePath;
	}
	
	 /**
     * 保存图片到sd卡
     */
	public  static String saveMyBitmap2(String bitName, int percent,Bitmap bmp) throws IOException {
		String filePath = newPath + bitName + ".jpg" ;
		
		File f = new File(filePath);
		f.createNewFile();
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		bmp.compress(Bitmap.CompressFormat.JPEG, percent, fOut);
		try {
			fOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filePath;
	}

	// Drawable->Bitmap
	public static Bitmap drawable2Bitmap(Drawable d) {
		if (d != null) {
			int width = d.getIntrinsicWidth();
			int height = d.getIntrinsicHeight();
			Bitmap.Config config = d.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
					: Bitmap.Config.RGB_565;
			Bitmap bitmap = Bitmap.createBitmap(width, height, config);
			Canvas canvas = new Canvas(bitmap);
			d.setBounds(0, 0, width, height);
			d.draw(canvas);
			return bitmap;
		}
		return null;
	}
}
